package com.JL.window;

import com.JL.engine.GameWorld;
import com.JL.engine.model.*;

import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Point2D;

public class GameWindow extends JFrame {
    //创建游戏世界
    private GameWorld gameWorld = new GameWorld();
    public GameWindow(){
        setTitle("坦克大战");
        //设置窗口位置大小
        setSize(800,600);
        //设置窗口位置居中
        setLocationRelativeTo(null);
        //设置默认关闭方式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置窗口不可调整大小
        setResizable(false);
        //将窗口默认面板设置为游戏世界
        setContentPane(gameWorld);
        //设置窗口可见
        setVisible(true);
        //使游戏世界获得焦点
        gameWorld.requestFocus();
        initGame();
        initInput();
        collision();// 注册碰撞检测监听器
    }
    public Entity createEntity(String entityName){
        return gameWorld.createEntity(entityName);
    }

    public Entity createEntity(String entityName,double x, double y){
        return gameWorld.createEntity(entityName,x,y);
    }

    public Entity createEntity(String entityName, InitData initData){
        return gameWorld.createEntity(entityName,initData);
    }

    public void onMousePressed(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //鼠标点击窗口任意位置
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void addCollisionHandler(Enum source, Enum target, CollisionHandler collisionHandler){
        gameWorld.addCollisionHandler(source, target, collisionHandler);
    }

    public void onMouseReleased(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void onMouseMoved(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity == null||entity.contains(e.getPoint())) {
                    listener.handler(e,entity);
                }
            }
        });
    }

    public void onMouseEntered(Entity entity, GameMouseListener listener) {
        gameWorld.addMouseMotionListener(new MouseAdapter() {

            private Point2D point;

            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity.contains(e.getPoint()) && point != null && !entity.contains(point)) {
                    listener.handler(e,entity);
                }
                point = e.getPoint();
            }
        });
    }
    public void onMouseDragged(Entity entity, GameMouseDraggedListener listener){
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            private Point2D point;

            public void mouseDragged(MouseEvent e){
                if(entity==null||entity.contains(e.getPoint())){
                    if(point==null){
                        point = e.getPoint();
                    }
                    listener.handler(e,point,entity);
                }
                point = e.getPoint();
            }
        });
    }
    public  void  onMouseExited(Entity entity, GameMouseListener listener){
        gameWorld.addMouseMotionListener(new MouseAdapter() {
            private Point2D point;
            public void mouseMoved(MouseEvent e){
                if(!entity.contains(e.getPoint()) && point != null && entity.contains(point)){
                    listener.handler(e,entity);
                }
                point = e.getPoint();

            }
        });
    }
    public <E extends MouseEvent>void onMouseWheelMoved(Entity entity, GameMouseListener<E> listener)
    {
        gameWorld.addMouseWheelListener(new MouseAdapter() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (entity==null||entity.contains(e.getPoint())){
                    listener.handler((E)e,entity);
                }
            }
        });
    }
    public void onMouseClicked(Entity entity,GameMouseListener listener){
        gameWorld.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (entity==null||entity.contains(e.getPoint())){
                    listener.handler(e,entity);
                }
            }
        });
    }
    public void onKey(int keyCode, GameKeyListener listener){
        gameWorld.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==keyCode){
                    listener.keyPressed(e);
                }
            }
            public void keyReleased(KeyEvent e){
                if (e.getKeyCode()==keyCode){
                    listener.keyRelesaed(e);
                }
            }
        });
    }
    public void initGame(){}
    public void initInput(){}
    public void collision(){
    }
    public void addEntity(Entity entity){
        this.gameWorld.addEntity(entity);
    }

    public void addEntities(Entity...entities){
        for(Entity entity : entities){
            addEntity(entity);
        }
    }

    public static void registerEntityFactory(EntityFactory factory){
        GameWorld.registerEntityFactory(factory);

    }
    public GameWorld getGameWorld(){return gameWorld;}
}
